import sys
import time
import signal
import logging

import log2network as log2net

hdlr = log2net.SocketServerHandler('127.0.0.1', 1730)
hdlr.setLevel(log2net.INFO)
hdlr.setFormatter(log2net.defaultFormatter)

console = logging.StreamHandler()
console.setLevel(log2net.DEBUG)
console.setFormatter(log2net.defaultFormatter)

logger = logging.getLogger("server11111")
logger.setLevel(log2net.DEBUG)
logger.addHandler(hdlr)
logger.addHandler(console)

logger2 = logging.getLogger("test2")
logger2.setLevel(log2net.DEBUG)
logger2.addHandler(hdlr)
logger2.addHandler(console)

def _exit(signum, frame):
    logger.removeHandler(hdlr)
    hdlr.close()
    #tcpHandler.shutdown()
    sys.exit()

@log2net.register("debug", "测试命令")
def cmd():
    #logger.setLevel(log2net.DEBUG)
    hdlr.setLevel(log2net.DEBUG)

@log2net.register("help", "命令列表")
def help():
    fmt = "\ncommond : %s\n\ttopic  : %s"
    msg = ""
    for cmd , value in log2net.commends().items():
        func, topic = value

        msg += fmt%(cmd, topic)
    logger.info(msg)


#log2net.SocketCommend.register("debug", cmd)

signal.signal(signal.SIGINT, _exit)
signal.signal(signal.SIGTERM, _exit)

while(True):
    logger.debug("")

    logger.info("")

    logger.warn("")

    logger.critical("")

    logger2.debug("")

    logger2.info("")

    logger2.warn("")

    logger2.critical("")
    time.sleep(1)


